﻿using DayuCode.Designer.Entity;
using DayuCode.Designer.Forms.template;
using DayuCode.Designer.Service;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DayuCode.Designer.Forms
{
    public partial class FormCodeTemplates : UIForm
    {
        private string terminalType;
        public FormCodeTemplates()
        {
            InitializeComponent();
            dgvTemplateFile.AutoGenerateColumns = false;
            
        }

        private void rdoJava_CheckedChanged(object sender, EventArgs e)
        {
            if (rdoJava.Checked)
            {
                terminalType = "COMMON";
                loadPageLayouts("COMMON", null);
            }
        }

        private void rdoWeb_CheckedChanged(object sender, EventArgs e)
        {
            if (rdoWeb.Checked)
            {
                terminalType = "WEB";
                loadPageLayouts("WEB", null);
            }
        }

        private void rdoAPP_CheckedChanged(object sender, EventArgs e)
        {
            if (rdoAPP.Checked)
            {
                terminalType = "APP";
                loadPageLayouts("APP", null);
            }
        }

        private void rdoPad_CheckedChanged(object sender, EventArgs e)
        {
            if (rdoPad.Checked)
            {
                terminalType = "PAD";
                loadPageLayouts("PAD", null);
            }
        }

        private void loadPageLayouts(string terminalType, long? selectedPageLayoutId)
        {
            var pageLayouts = TemplatePageLayoutHandler.getTemplatePageLayouts(terminalType);
            listPageLayout.DataSource = pageLayouts;
            if(selectedPageLayoutId == null && pageLayouts.Count > 0)
            {
                listPageLayout.SelectedIndex = 0;
            }
            if(selectedPageLayoutId != null)
            {
                int idx = pageLayouts.IndexOf(new TemplatePageLayout(selectedPageLayoutId));
                listPageLayout.SelectedIndex = idx;
            }
        }

        private void listPageLayout_SelectedIndexChanged(object sender, EventArgs e)
        {
            loadPageTemplates();
        }

        private void loadPageTemplates()
        {
            TemplatePageLayout pageLayout = listPageLayout.SelectedItem as TemplatePageLayout;
            if (pageLayout == null)
            {
                dgvTemplateFile.DataSource = null;
                return;
            }

            var templates = CodeTemplateHandler.getCodeTemplatesByLayout((long)pageLayout.Id);
            dgvTemplateFile.DataSource = templates;
        }

        private void btnAddPageLayout_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(terminalType)) return;
            
            FormPageLayout form = new FormPageLayout(terminalType, null);
            form.Owner = this;
            if(form.ShowDialog(this) == DialogResult.OK)
            {
                TemplatePageLayout pageLayout = form.TemplatePageLayout;
                loadPageLayouts(terminalType, pageLayout.Id);
            }
            form.Dispose();
        }

        private void btnEditPageLayout_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(terminalType)) return;
            TemplatePageLayout pageLayout = listPageLayout.SelectedItem as TemplatePageLayout;
            if (pageLayout == null)
            {
                return;
            }
            long pageLayoutId = (long)pageLayout.Id;
            FormPageLayout form = new FormPageLayout(terminalType, pageLayoutId);
            form.Owner = this;
            if (form.ShowDialog(this) == DialogResult.OK)
            {
                pageLayout = form.TemplatePageLayout;
                loadPageLayouts(terminalType, pageLayoutId);
            }
            form.Dispose();
        }

        private void btnDelPageLayout_Click(object sender, EventArgs e)
        {
            TemplatePageLayout pageLayout = listPageLayout.SelectedItem as TemplatePageLayout;
            if (pageLayout == null)
            {
                return;
            }
            if (this.ShowAskDialog("确定要删除布局【"+ pageLayout .LayoutName+ "】吗？"))
            {
                long pageLayoutId = (long)pageLayout.Id;
                TemplatePageComponentHandler.deleteTemplatePageComponent(pageLayoutId);
                this.ShowSuccessTip("已删除！");
                loadPageLayouts(terminalType, null);
            }
        }

        private void btnAddCodeTemplate_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(terminalType)) return;
            TemplatePageLayout pageLayout = listPageLayout.SelectedItem as TemplatePageLayout;
            if (pageLayout == null)
            {
                return;
            }
            long pageLayoutId = (long)pageLayout.Id;

            FormCodeTemplate form = new FormCodeTemplate(pageLayoutId, null);
            form.Owner = this;
            if (form.ShowDialog(this) == DialogResult.OK)
            {
                loadPageTemplates();
            }
            form.Dispose();
        }

        private void btnEditCodeTemplate_Click(object sender, EventArgs e)
        {
            editSelectedRow();
        }

        private void editSelectedRow()
        {
            if (dgvTemplateFile.SelectedRows.Count == 0) return;
            CodeTemplate codeTemplate = dgvTemplateFile.SelectedRows[0].DataBoundItem as CodeTemplate;
            FormCodeTemplate form = new FormCodeTemplate(0, codeTemplate.Id);
            form.Owner = this;
            if (form.ShowDialog(this) == DialogResult.OK)
            {
                loadPageTemplates();
            }
            form.Dispose();
        }

        private void btnDelCodeTemplate_Click(object sender, EventArgs e)
        {
            if (dgvTemplateFile.SelectedRows.Count == 0) return;
            CodeTemplate codeTemplate = dgvTemplateFile.SelectedRows[0].DataBoundItem as CodeTemplate;
            if (this.ShowAskDialog("确定要删除模板文件【" + codeTemplate.FilePath + "】吗？"))
            {
                CodeTemplateHandler.deleteCodeTemplate((long)codeTemplate.Id);
                this.ShowSuccessTip("已删除！");
                loadPageTemplates();
            }
        }

        private void uiButton1_Click(object sender, EventArgs e)
        {
            FormCodeFragments form = new FormCodeFragments();
            form.Owner = this;
            form.ShowDialog(this);
            form.Dispose();
        }

        private void dgvTemplateFile_DoubleClick(object sender, EventArgs e)
        {
            editSelectedRow();
        }
    }
}
